package com.tiger.utils;
 
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author HuXuehao (StudiousTiger)
 * @desc 这个类的作用是-时间工具类
 * @date 2022/2/21
 */
public class DateTimeUtils {
     private static String pattern = "yyyy-MM-dd HH:mm:ss";
    private static String pattern2 = "yyyy-MM-dd";

    /**
     * 当前sql类型Timestamp的当前时间
     * @return
     */
    public static Timestamp currentDateTime2SQLTimestamp(){
        Date utilDate = new Date();
        Timestamp sqlDate = new Timestamp(utilDate.getTime());
        return sqlDate;
    }

    /**
     * 数据库datetime转String
     * @param sqlDateTime
     * @return
     */
    public static String sqlDateTime2String(Timestamp sqlDateTime){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(sqlDateTime);
    }

    // 将long类型格式化
    public static String long2FormString(long lo) {
        Date date = new Date(lo);
        SimpleDateFormat sd = new SimpleDateFormat(pattern);
        return sd.format(date);
    }
    /**
     * 将数据库 Timestamp 格式化 (yyyy-MM-dd HH:mm:ss)
     * @param sqlDateTime
     * @return
     */
    public static String sqlTimestamp2FormStr(Timestamp sqlDateTime){
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(sqlDateTime);
    }

    /**
     * 获取当前已经格式化后的时间 (yyyy-MM-dd HH:mm:ss)
     * @return
     */
    public static String currentDatetimeOfFormStr(){
        Timestamp timestamp = currentDateTime2SQLTimestamp();
        return sqlTimestamp2FormStr(timestamp);
    }

    /**
     * 获取当前已经格式化后的日期 (yyyyMMdd)
     * @return
     */
    public static String currentDateOfFormStr(){
        Timestamp timestamp = currentDateTime2SQLTimestamp();
        SimpleDateFormat sdf = new SimpleDateFormat(pattern2);
        return sdf.format(timestamp);
    }


}